using UnityEngine;
using System.Collections;
using System.Linq;

public class Aircraft : MonoBehaviour {

    public Texture2D[] planeStateColors;
    public enum FlightPhase { departing, flying, holding, landing }

    public FlightPhase flightPhase = FlightPhase.departing;
    public float speed = 1.0f;
    public Airport departureAirport;//TODO: Set this when aeroplane departs form airport
    public Airport destinationAirport;
    public Constants.Locations DestinationAirport {
        get {
            return destinationAirport.airportName;
        }set {
            destinationAirport = GetAirportForDestination(value);
        }
    }

    public void Start() {
        transform.position = new Vector3(transform.position.x, Constants.DRAW_ORDER_PLANE, transform.position.z);

    }

    public void DetermineDestinationAirport() {
        Constants.Locations potentialDestinationAirport;
        do {
            potentialDestinationAirport = (Constants.Locations)Random.Range(0, 5);
        } while (potentialDestinationAirport == departureAirport.airportName);

        destinationAirport = GetAirportForDestination(
            potentialDestinationAirport
        );
    }

    Airport GetAirportForDestination( Constants.Locations destinationName ) {
        Airport[] airports = FindObjectsOfType(typeof(Airport)) as Airport[];
        return airports.First(a => (a as Airport).airportName == destinationName) as Airport;
    } 

	// Use this for initialization
	public void TakeOffAeroplane (Airport departureAirport) {
        destinationAirport.inboundPlanes.Add(this);
        Vector3 departureAirportLocation = departureAirport.transform.position;
        flightPhase = FlightPhase.flying;
	}

    public void RecievePermissionToLand() {
        Debug.Log(transform.gameObject.name + " Recieved permission to land");
        flightPhase = FlightPhase.landing;
        Interface.activeAircraft = this;
    }

    void Update() {
        gameObject.renderer.material.mainTexture = planeStateColors[(int)flightPhase];

        if (Constants.gameState != Constants.GameState.Planning) {
            if (this.flightPhase == FlightPhase.holding || flightPhase == FlightPhase.flying) {
                Score.cost += 2;
            } else {
                Score.cost += 1;
            }
        }
        if (destinationAirport != null) {
            //check if we are in holding
            float distFromAirport = (transform.position - destinationAirport.gameObject.transform.position).sqrMagnitude ;
            bool inHoldingRange = distFromAirport < (destinationAirport.planesInHolding.Count + 1) * 1.0f;

            if (inHoldingRange && flightPhase != FlightPhase.holding && flightPhase != FlightPhase.landing) {
                flightPhase = FlightPhase.holding;
                StartCoroutine(destinationAirport.EnterHoldingPattern(this));
            } else if(flightPhase == FlightPhase.flying || flightPhase == FlightPhase.landing) {
                transform.LookAt(destinationAirport.gameObject.transform.position);
                //fly toward airport
                transform.position = Vector3.MoveTowards(
                                        transform.position,
                                        destinationAirport.gameObject.transform.position,
                                        speed * Time.deltaTime);
            } 
            
            if (flightPhase == FlightPhase.landing && distFromAirport < 0.05f) {
                Score.landedPlanes++;
                destinationAirport.inboundPlanes.Remove(this);
                //departureAirport.outboundPlanes.Remove(this);
                gameObject.SetActive(false);
                Score.CheckGameDone();
            }
        } //else { Debug.LogError("Plane without Destination"); }
    }
	
}